package com.lai.note.service.impl;

import com.lai.note.dao.UserMapper;
import com.lai.note.dto.UserDto;
import com.lai.note.exception.*;
import com.lai.note.model.User;
import com.lai.note.service.UserBaseService;
import com.lai.note.util.RandomUtil;
import com.lai.note.util.RedisTemplateUtil;
import com.lai.note.util.SHA256Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserBaseServiceImpl implements UserBaseService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public User userRegister(String phone, String password,Double lng,Double lat) throws UserExistException {
        if(userMapper.selectUserByPhone(phone)!=null){
            throw new UserExistException();
        }
        User user = new User();
        user.setUserId(RandomUtil.getUUID());
        user.setUserName("");
        user.setPhone(phone);
        user.setLoginPwd(SHA256Utils.getSHA256(password));
        user.setEmail("");
        user.setIdentity("");
        user.setAge(0);
        user.setSex(false);
        user.setAddress("");
        user.setUserAuth(false);
        user.setCredit(0);
        user.setIconPath("http://qv8hg5jct.hn-bkt.clouddn.com/user1.jpg");//默认头像路径
        user.setSignature("");
        user.setBirthday("");
        user.setLongitude(lng);
        user.setLatitude(lat);
        user.setInternalTest(false);
        user.setRegisterDate(LocalDateTime.now());
        System.out.println("即将要插入的user为："+user);
        userMapper.addUser(user);
        return user;
    }

    @Override
    public User updateUser(UserDto userDto) throws DataUpdateFailureException, UserNotExistException {
        User user = userMapper.selectByPrimaryKey(userDto.getUserId());
        if (user==null){
            throw new UserNotExistException();
        }
        user.setUserName(userDto.getUserName());
        user.setPhone(userDto.getPhone());
        user.setEmail(userDto.getEmail());
        user.setAge(userDto.getAge());
        user.setSex(userDto.getSex().equals("男"));//男1 女0
        user.setAddress(userDto.getAddress());
        user.setIconPath(userDto.getIconPath());
        user.setSignature(userDto.getSignature());
        user.setBirthday(userDto.getBirthday());

        //更新
        int res = userMapper.updateByPrimaryKeySelective(user);
        if(res==0){
            throw new DataUpdateFailureException();
        }

        return user;
    }

    @Override
    public User userLoginByPwd(String phone, String pwd) throws NoPassIdentityException, UserNotExistException {
        System.out.println("进来了\nphone:"+phone);
        User user = userMapper.selectByPhone(phone);
        if (user == null)
            throw new UserNotExistException();
        if (!user.getLoginPwd().equals(pwd))
            throw new NoPassIdentityException();
        return user;
    }

    @Override
    public User userLoginByCode(String phone, String code) throws UserNotExistException, IdentifyingCodeException {
        String redisCode = (String)redisTemplateUtil.get(phone);
        logger.info("redis的验证码："+redisCode+"--传进来的验证码："+code);
        if (!code.equals(redisCode))
            throw new IdentifyingCodeException();
        User user = userMapper.selectByPhone(phone);
        if (user == null)
            throw new UserNotExistException();
        return user;
    }


    @Override
    public User findUserById(String userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public boolean updateUserLoginPwdByOldPwd(String userId, String oldPwd, String newPwd) throws NoPassIdentityException {
        User user = userMapper.selectByPrimaryKey(userId);
        System.out.println("user:"+user);
        if(!user.getLoginPwd().equals(oldPwd)){
            throw new NoPassIdentityException();
        }
        user.setLoginPwd(newPwd);
        if(userMapper.updateByPrimaryKeySelective(user)==1)
            return true;
        return false;
    }

    @Override
    public boolean updateUserLoginPwdByCode(String phone, String code, String newPwd) throws IdentifyingCodeException {
        User user = userMapper.selectByPhone(phone);
        String redisCode = (String) redisTemplateUtil.get(phone);
        System.out.println("redisCode："+redisCode);

        if(!code.equals(redisCode)){
            throw new IdentifyingCodeException();
        }
        user.setLoginPwd(newPwd);
        if(userMapper.updateByPrimaryKeySelective(user)==1)
            return true;
        return false;
    }
}
